package net.enclosing.basecamp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

public class ToDoList {
	String id = "";
	int projectid = 0;
	int milestoneid = 0;
	Milestone milestone = null;
	String name = "";
	Project project = null;
	public String toString(){
		StringBuilder builder = new StringBuilder();
		StringBuilder html= new StringBuilder();
		builder.append("\r\n=== ");
//		builder.append(id);
		builder.append(project!=null? project.getName()+"/":"");
		builder.append(milestone!=null? milestone.getName()+"/":"");
		builder.append(name);
		builder.append(" ============= \r\n");
		builder.append("\r\n\r\n");

		

//		builder.append("<br />");
		return builder.toString();
		
	}

	public static void main(String[] args) {
		try {
			
			List<ToDoList> toDoLists = ToDoList.recents();
			Project dealsProject = Project.create(5998553);
//			Project osProject = Project.create(5998553);

			List<ToDoList> dealsToDoLists = ToDoList.recentsOf(dealsProject);
//			List<ToDoList> osToDoLists = ToDoList.recentsOf(osProject);
			
			StringBuilder html = new StringBuilder();
			StringBuilder nodings = new StringBuilder();
			StringBuilder executbales = new StringBuilder();
			html.append("<html><head>        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">    <link href='base.css'  rel='stylesheet' type='text/css' /></head><body>");
			html.append("<a href='https://storytellers.basecamphq.com/milestones/responsibilities?responsible_party=1143796'>Basecamp</a>");
			html.append("<div style='text-align:left'>");
					
			html.append("<ul>");
			nodings.append(html.toString());
			executbales.append(html.toString());

			for (Iterator iterator = dealsToDoLists.iterator(); iterator
					.hasNext();) {
				ToDoList toDoList = (ToDoList) iterator.next();
				boolean contains = false;
				for (Iterator iterator2 = toDoLists.iterator(); iterator2
						.hasNext();) {
					ToDoList toDoList2 = (ToDoList) iterator2.next();
					if(toDoList2.getId().equals(toDoList.getId()))
					{
						contains = true;
						break;
					}
				}
				if(!contains){
					toDoLists.add(toDoList);
				}
			}
//			toDoLists.addAll(dealsToDoLists);
//r			toDoLists.addAll(osToDoLists);
			
			for (ToDoList toDoList : toDoLists) {
				
				List<Todo> todos = Todo.todosOf(toDoList);
				StringBuilder listBuilder = new  StringBuilder();
				if(todos.size()>0){
					
					listBuilder.append("<li>");
					String milestonename = toDoList.milestone!=null?toDoList.milestone.getName():"";
					String projectname = toDoList.project!=null?toDoList.project.getName():"";
					listBuilder.append("<a href='https://storytellers.basecamphq.com/projects/"+toDoList.project.getId()+"/todo_lists/"+toDoList.getId()+"'>");
					listBuilder.append(toDoList.name);
					listBuilder.append("</a>");
					listBuilder.append(" : ");
					listBuilder.append("<a href='https://storytellers.basecamphq.com/projects/"+toDoList.project.getId()+"/milestones'>");
					listBuilder.append( milestonename);
					listBuilder.append("</a>");
					listBuilder.append( " : " );
					listBuilder.append("<a href='https://storytellers.basecamphq.com/projects/5965592/log'>");
					listBuilder.append( projectname);
					listBuilder.append("</a>");
					
					
					System.err.println(toDoList.toString());
					listBuilder.append("<ul>");
					nodings.append(listBuilder.toString());
					executbales.append(listBuilder.toString());
					for (Todo todo : todos) {
						
						listBuilder.append("<li>");
						listBuilder.append("<a href='https://storytellers.basecamphq.com/projects/"+toDoList.project.getId()+"/todo_items/"+todo.getId()+"/comments'>");
						listBuilder.append(todo.toString());
						listBuilder.append("</a>");
						listBuilder.append("</li>");
						if(todo.getName().contains("@Noding")){
							nodings.append("<li>");
							nodings.append("<a href='https://storytellers.basecamphq.com/projects/"+toDoList.project.getId()+"/todo_items/"+todo.getId()+"/comments'>");
							nodings.append(todo.toString());
							nodings.append("</a>");
							nodings.append("</li>");
							
						}else{
							executbales.append("<li>");
							executbales.append("<a href='https://storytellers.basecamphq.com/projects/"+toDoList.project.getId()+"/todo_items/"+todo.getId()+"/comments'>");
							executbales.append(todo.toString());
							executbales.append("</a>");
							executbales.append("</li>");
							
						}
						
					}
					listBuilder.append("</ul>");
					listBuilder.append("</li>");
					nodings.append("</ul>");
					nodings.append("</li>");
					executbales.append("</ul>");
					executbales.append("</li>");
					
				}
				html.append(listBuilder.toString());
			}
			html.append("</ul>");
			html.append("</div>");
			html.append("</html></body>");
			nodings.append("</ul>");
			nodings.append("</div>");
			nodings.append("</html></body>");
			executbales.append("</ul>");
			executbales.append("</div>");
			executbales.append("</html></body>");
			
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File("nodings.html")),"UTF-8");
			writer.write(nodings.toString());
			writer.flush();
			writer.close();
			 writer = new OutputStreamWriter(new FileOutputStream(new File("executables.html")),"UTF-8");
			writer.write(executbales.toString());
			writer.flush();
			writer.close();
			 writer = new OutputStreamWriter(new FileOutputStream(new File("recents.html")),"UTF-8");
			writer.write(html.toString());
			writer.flush();
			writer.close();
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	
	public static List<ToDoList> recents() throws ParseException {
		
		Collection<Project> recentProjects = Project.recents();
		ArrayList<ToDoList> todolists = new ArrayList<ToDoList>();
		for (Project project : recentProjects) {
			List<ToDoList> todolistsOfAProject = ToDoList.recentsOf(project);
			List<Milestone> milestonesOfAProject = Milestone.recentsOf(project);
			boolean flag = false;
			for (ToDoList toDoList : todolistsOfAProject) {
				if(isMilestoned(milestonesOfAProject, toDoList)){
					todolists.add(toDoList);
				}
			}
		}
		return todolists;
	}
	private static boolean isMilestoned(List<Milestone> milestonesOfAProject,
			ToDoList toDoList) {
		for (Milestone milestone : milestonesOfAProject) {
			if(String.valueOf(toDoList.milestoneid).equals(milestone.id)){
				toDoList.setMilestone(milestone);
				return true;
			}
		}
		return false;
	}
	private static List<ToDoList> recentsOf(Project p) throws ParseException {
		LocalStaticData staticData = new LocalStaticData();
		String path = "projects/" + p.getId() + "/todo_lists.xml";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Document document =  Basecamp.get(path);
		Element todolistsElement =  document.getRootElement();
		List<Element> todolistElements = todolistsElement.elements();
		ArrayList<ToDoList> todolists = new ArrayList<ToDoList>();
		Date recentThresholdDate = Basecamp.recentThresholdDate(); 
		for (Element todolistElement : todolistElements) {
			ToDoList todolist = new ToDoList();
			if(todolistElement.element("complete").getText().equals("false")  
					){
				todolist.setId(todolistElement.elementText("id"));
				todolist.setName(todolistElement.elementText("name"));
				todolist.setProjectid(Integer.valueOf(todolistElement.elementText("project-id")).intValue());
				
				todolist.setProject(p);
				if(!todolistElement.elementText("milestone-id").equals("")){
					todolist.setMilestoneid(Integer.parseInt(todolistElement.elementText("milestone-id")));
				}
				todolist.setProject(p);
				todolists.add(todolist);
			}
		}
		return todolists;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	public int getMilestoneid() {
		return milestoneid;
	}

	public void setMilestoneid(int milestoneid) {
		this.milestoneid = milestoneid;
	}

	public Milestone getMilestone() {
		return milestone;
	}

	public void setMilestone(Milestone milestone) {
		this.milestone = milestone;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getProjectid() {
		return projectid;
	}
	public void setProjectid(int projectid) {
		this.projectid = projectid;
	}
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}

}
